<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18" class="mb20">
						<el-form-item label="用户名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入用户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18" class="mb20">
						<el-form-item label="手机号" prop="desc">
							<el-input v-model="state.ruleForm.desc" type="text" placeholder="请输入用户手机号" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18" class="mb20">
            <el-form-item label="密码" prop="price">
              <el-input v-model="state.ruleForm.price" type="text" placeholder="请输入用户密码" maxlength="150"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18" class="mb20">
            <el-form-item label="密码" prop="category_id">
              <el-input v-model="state.ruleForm.category_id" type="text" placeholder="请输入用户密码" maxlength="150"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="18" :lg="18" :xl="18" class="mb20">
            <el-form-item label="用户标识" prop="files">
              <template #label>
                <span>角色</span>
              </template>
              <el-select v-model="state.ruleForm.files" multiple clearable placeholder="Select" style="width: 380px" >
                <el-option
                    v-for="item in roles"
                    :key="item.id"
                    :label="item.role"
                    :value="item.role"
                />
              </el-select>
            </el-form-item>
          </el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="resetForm" size="default">取消</el-button>
					<el-button type="primary" @click="submitForm" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { reactive, ref, onMounted } from 'vue';
import type { FormInstance, FormRules } from 'element-plus'

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const props = defineProps({
  roles: {
    type: Array,
    default: []
  }
});

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		name: '', // 用户名称
    desc: "", // 手机号
    price: "", // 密码
    files: [],
    id: "",
    category_id: ""
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const clearForm = () => {
  state.ruleForm.name = ""
  state.ruleForm.desc = ""
  state.ruleForm.price = ""
  state.ruleForm.id = ""
  state.ruleForm.category_id = ""
  state.ruleForm.files.splice(0, state.ruleForm.files.length)
}

const rules = reactive<FormRules>({
  name: [
    { required: true, message: '请输入名字', trigger: 'blur' },
    { max: 20, message: '名字最长20位', trigger: 'blur' },
  ],
  desc: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
  ],
  price: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  files: [
    { required: true, message: '请选择授权角色', trigger: 'blur' },
  ],
  category_id: [
    { required: true, message: '请选择授权角色', trigger: 'blur' },
  ],
})

// 打开弹窗
const openDialog = (type: string, row: any) => {
	if (type === 'edit') {
		// state.ruleForm = row;
		// state.ruleForm.password = "123456";
		// state.ruleForm.name = row.name;
		// state.ruleForm.mobile = row.mobile;
		// state.ruleForm.roles = row.roles;
		// state.ruleForm.id = row.id;
		// state.dialog.title = '修改用户';
		// state.dialog.submitTxt = '修改';
	} else {
		state.dialog.title = '新增食物';
		state.dialog.submitTxt = '新增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	roleDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = async () => {
  await roleDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};

const ruleFormRef = ref<FormInstance>()
const submitForm = async () => {
  if (!roleDialogFormRef.value) return
  await roleDialogFormRef.value.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      emit('refresh', state.ruleForm, state.dialog.submitTxt);
      closeDialog();
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = async () => {
  if (!roleDialogFormRef.value) return
  await roleDialogFormRef.value.resetFields()
  await closeDialog();
}

// 暴露变量
defineExpose({
	openDialog,
  clearForm
});
</script>

<style scoped lang="less">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
